#include <stdio.h>
#include <string.h>
#include <display_manager.h>

static PT_displayOpr g_ptDisplayOprHead;
static PT_displayOpr g_ptSelectedDisplayOpr;

int registerDisplayOpr(PT_displayOpr ptDisplayOpr)
{
	PT_displayOpr temp = g_ptDisplayOprHead;

	if(ptDisplayOpr ==NULL)
	{
		fprintf(stderr,"error the argument is null\n");
		return -1;
	}

	if(g_ptDisplayOprHead ==NULL)
		g_ptDisplayOprHead = ptDisplayOpr;
	else
	{
		while(temp->next!=NULL)
		{
			temp = temp->next;
		}
		temp->next = ptDisplayOpr;
	}

	return 0;
}

void showDisplayOpr(void)
{
	int i=0;
	PT_displayOpr temp = g_ptDisplayOprHead;
	
	while(temp!=NULL)
	{
		fprintf(stdout, "%02d %s\n", i++, temp->name);
		temp = temp->next;
	}
}

PT_displayOpr getDisplayOpr(char *name)
{
	PT_displayOpr temp = g_ptDisplayOprHead;

	while(temp!=NULL)
	{
		if(strcmp(temp->name,name)==0)
		{
			g_ptSelectedDisplayOpr = temp;
			temp->displayDeviceInit();
			return temp;
		}
		
		temp = temp->next;
	}

	return NULL;
}

int displayInit(void)
{
	int ret;

	//init fb
	ret = fbInit();
	
	return ret;
}

int displayExit(void)
{
	int ret;

	if((g_ptSelectedDisplayOpr->displayDeviceExit)==NULL)
		return 0;
	else
		ret = g_ptSelectedDisplayOpr->displayDeviceExit();
	
	return ret;
}

